package org.dromara.cms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.cms.domain.vo.CmsModelFieldVo;
import org.dromara.cms.domain.bo.CmsModelFieldBo;
import org.dromara.cms.service.ICmsModelFieldService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * CMS扩展字段
 *
 * @author Spring
 * @date 2024-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/modelField")
public class CmsModelFieldController extends BaseController {

    private final ICmsModelFieldService cmsModelFieldService;

    /**
     * 查询CMS扩展字段列表
     */
    @SaCheckPermission("cms:modelField:list")
    @GetMapping("/list")
    public TableDataInfo<CmsModelFieldVo> list(CmsModelFieldBo bo, PageQuery pageQuery) {
        return cmsModelFieldService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CMS扩展字段列表
     */
    @SaCheckPermission("cms:modelField:export")
    @Log(title = "CMS扩展字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsModelFieldBo bo, HttpServletResponse response) {
        List<CmsModelFieldVo> list = cmsModelFieldService.queryList(bo);
        ExcelUtil.exportExcel(list, "CMS扩展字段", CmsModelFieldVo.class, response);
    }

    /**
     * 获取CMS扩展字段详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cms:modelField:query")
    @GetMapping("/{id}")
    public R<CmsModelFieldVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(cmsModelFieldService.queryById(id));
    }

    /**
     * 新增CMS扩展字段
     */
    @SaCheckPermission("cms:modelField:add")
    @Log(title = "CMS扩展字段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmsModelFieldBo bo) {
        return toAjax(cmsModelFieldService.insertByBo(bo));
    }

    /**
     * 修改CMS扩展字段
     */
    @SaCheckPermission("cms:modelField:edit")
    @Log(title = "CMS扩展字段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmsModelFieldBo bo) {
        return toAjax(cmsModelFieldService.updateByBo(bo));
    }

    /**
     * 删除CMS扩展字段
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cms:modelField:remove")
    @Log(title = "CMS扩展字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(cmsModelFieldService.deleteWithValidByIds(List.of(ids), true));
    }
}
